package com.taptech.doufu.ui.view;

import android.animation.Animator;
import android.animation.AnimatorInflater;
import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.taptech.doufu.R;
import com.taptech.doufu.bean.GiftBean;
import com.taptech.doufu.util.ImageManager;
import com.taptech.doufu.util.ScreenUtil;
import com.taptech.doufu.util.TMAnalysis;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by longsiyang on 16/1/15.
 */
public class GiftDisplayView extends LinearLayout implements View.OnTouchListener  {

    LinearLayout topLayout, bottomLayout;
    Context mContext;
    LinearLayout.LayoutParams layoutParams;
    int pagePos = 0;
    List<Integer> viewIdList = new ArrayList<>();
    List<View> giftViewList = new ArrayList<>();
    List<GiftBean> mDataList;
    List<ImageView> coverImgList = new ArrayList<>();

    OnSelectListener mOnSelectListener;
    int currentSelectPos = 0;
    // 触摸持续时间
    int touchLastTime = 0;
    // 100ms 发一次 10次 算一秒 就算点击了
    final int TouchEqualClick = 10;
    float[] actionDownXY = new float[2];
    Runnable timeRunable = new Runnable() {
        @Override
        public void run() {
            if (timeHandler != null){
                touchLastTime ++;
                // 判定为长按
                if (touchLastTime > TouchEqualClick) {
                    mOnSelectListener.onSelectd(currentSelectPos);
                    viewPressingAnimation(giftViewList.get(currentSelectPos - 6*pagePos).findViewById(R.id.gift_image_layout));
                }
                timeHandler.postDelayed(this , 100);
            }
        }
    };


    public GiftDisplayView(Context context) {
        super(context);
        mContext = context;
        initView();
    }

    public GiftDisplayView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        initView();
    }

    public GiftDisplayView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        initView();
    }

    private void initView() {
        View view = null;
        this.setOrientation(VERTICAL);
        topLayout = new LinearLayout(mContext);
        layoutParams = new LinearLayout.LayoutParams(ScreenUtil.dip2px(mContext , 300), 0);
        layoutParams.weight = 1;
        layoutParams.gravity = Gravity.CENTER;
        topLayout.setLayoutParams(layoutParams);
        topLayout.setOrientation(HORIZONTAL);
        topLayout.setGravity(Gravity.LEFT);
        this.addView(topLayout);
        for (int i = 0; i < 3; i++) {
            view = LayoutInflater.from(mContext).inflate(R.layout.gift_dialog_item_layout, topLayout, false);

            view.setOnTouchListener(this);
            view.setVisibility(GONE);
            giftViewList.add(view);
            viewIdList.add(view.getId());
            topLayout.addView(view);
        }

        bottomLayout = new LinearLayout(mContext);
        layoutParams = new LinearLayout.LayoutParams(ScreenUtil.dip2px(mContext , 300), 0);
        layoutParams.weight = 1;
        layoutParams.gravity = Gravity.CENTER;
        bottomLayout.setLayoutParams(layoutParams);
        bottomLayout.setOrientation(HORIZONTAL);
        bottomLayout.setGravity(Gravity.LEFT);
        this.addView(bottomLayout);
        for (int i = 0; i < 3; i++) {
            view = LayoutInflater.from(mContext).inflate(R.layout.gift_dialog_item_layout, bottomLayout, false);

            view.setOnTouchListener(this);
            view.setVisibility(GONE);
            giftViewList.add(view);
            viewIdList.add(view.getId());
            bottomLayout.addView(view);
        }
    }

    public void setContentInfo(List<GiftBean> dataList, int pagePosition, OnSelectListener onSelectListener) {
        if (dataList == null) {
            return;
        }
        mDataList = dataList;
        pagePos = pagePosition;
        mOnSelectListener = onSelectListener;
        for (int i = 0; i < (dataList.size() < 6 ? dataList.size() : 6); i++) {
            SimpleDraweeView giftIcon;
            TextView giftName, giftPrice;
            ImageView coverImg;
            if (i < 3) {
                giftIcon = (SimpleDraweeView) topLayout.getChildAt(i).findViewById(R.id.gift_icon);
                giftName = (TextView) topLayout.getChildAt(i).findViewById(R.id.gift_name);
                giftPrice = (TextView) topLayout.getChildAt(i).findViewById(R.id.gift_price);
                coverImg = (ImageView) topLayout.getChildAt(i).findViewById(R.id.cover_img);
                ImageManager.loadImage(giftIcon, dataList.get(i).getImage(), 1.0f);
                giftName.setText(dataList.get(i).getName());
                giftPrice.setText(dataList.get(i).getCoins());
            } else {
                giftIcon = (SimpleDraweeView) bottomLayout.getChildAt(i - 3).findViewById(R.id.gift_icon);
                giftName = (TextView) bottomLayout.getChildAt(i - 3).findViewById(R.id.gift_name);
                giftPrice = (TextView) bottomLayout.getChildAt(i - 3).findViewById(R.id.gift_price);
                coverImg = (ImageView) bottomLayout.getChildAt(i - 3).findViewById(R.id.cover_img);

                ImageManager.loadImage(giftIcon, dataList.get(i).getImage(), 1.0f);
                giftName.setText(dataList.get(i).getName());
                //giftPrice.setText(dataList.get(i).getCoins());
                giftPrice.setText(dataList.get(i).getCoins());
            }
            giftViewList.get(i).setVisibility(VISIBLE);
            coverImgList.add(coverImg);
        }

    }

    public void refreshView() {
        for (int i = 0; i < mDataList.size(); i++) {
            if (mDataList.get(i).isSelected()) {
                //giftViewList.get(i).setBackgroundColor(Color.parseColor("#44ff0000"));
                giftViewList.get(i).findViewById(R.id.cover_img).setVisibility(VISIBLE);
            } else {
                //giftViewList.get(i).setBackgroundColor(Color.parseColor("#ffffff"));
                giftViewList.get(i).findViewById(R.id.cover_img).setVisibility(GONE);
            }

        }

    }


    public interface OnSelectListener {
        void onSelectd(int position);

    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:

                if (touchLastTime == 0){
                    timeHandler.postDelayed(timeRunable, 100);
                    currentSelectPos = pagePos * 6 + giftViewList.indexOf(v);
                }
                actionDownXY[0] = event.getX();
                actionDownXY[1] = event.getY();
                setViewAnimation(v.findViewById(R.id.gift_image_layout) , true);
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                if (!isActionMove(actionDownXY[0] , actionDownXY[1] , event.getX() , event.getY())){
                    if (mOnSelectListener != null) {
                        mOnSelectListener.onSelectd(pagePos * 6 + giftViewList.indexOf(v));
                    }


                    for (int i = 0; i < mDataList.size(); i++) {
                        coverImgList.get(i).setVisibility(GONE);
                    }
                    v.findViewById(R.id.cover_img).setVisibility(VISIBLE);
                    Map keyMap = new HashMap();
                    keyMap.put("mid" , mDataList.get(giftViewList.indexOf(v)).getId());
                    keyMap.put("mpos" , pagePos * 6 + giftViewList.indexOf(v));
                    keyMap.put("mprice" ,mDataList.get(giftViewList.indexOf(v)).getCoins());
                    TMAnalysis.event(mContext , "select_item_gift");

                }
                setViewAnimation(v.findViewById(R.id.gift_image_layout) , false);
                touchLastTime = 0;
                if (timeHandler != null){
                    timeHandler.removeCallbacks(timeRunable);
                }
                break;
        }
        return true;
    }

    private boolean isActionMove(float oldX , float oldY , float x , float y){

        float distance = 20.0f;
        if (Math.abs(oldX - x) > distance){
            return true;
        }
        if (Math.abs(oldY - y) > distance){
            return true;
        }

        return false;
    }


    private Handler timeHandler = new Handler();


    private void setViewAnimation(View view  , boolean pressDown){
        Animator anim = null;
        if (pressDown){
            anim = AnimatorInflater.loadAnimator(mContext, R.animator.gift_item_anim_small);
        }else {
            anim = AnimatorInflater.loadAnimator(mContext, R.animator.gift_item_anim_big);
        }
        view.invalidate();
        anim.setTarget(view);
        anim.start();
    }

    private void viewPressingAnimation(View view){
        Animator anim = AnimatorInflater.loadAnimator(mContext , R.animator.gift_item_anim_pressing);
        view.invalidate();
        anim.setTarget(view);
        anim.start();
    }

}
